<!-- 底图切换组件 -->
<script setup>
import { onMounted, ref } from 'vue';
import BiaoZhuTu from '@/assets/images/base-map/biaozhutu.png';
import DaoLuTu from '@/assets/images/base-map/daolutu.png';
import HuWaiTu from '@/assets/images/base-map/huwaitu.png';
import LayerClose from '@/assets/images/base-map/layer-close.png';
import ShengSeTu from '@/assets/images/base-map/shengsetu.png';
import YingXiangTu from '@/assets/images/base-map/yingxiangtu.png';
import { useMapStore } from '@/stores';
import { LAYERS_GROUP_ID, TD_DL_URL, TD_IMG_URL, TD_CIA_URL } from '@/utils/map-helper';
import { GET_MAPBOX_MAP, REMOVE_MAPBOX_LAYER, MAP_STYLE_SWITCHER, MAP_STYLE_RESET, mapTo3d } from '@/utils/map-utils';

const emit = defineEmits(['handleChangeIsBaseLayer']);
const baseMapName = ref('影像图');
const mapStore = useMapStore();

onMounted(() => {
  baseMapName.value = mapStore.baseMapLayer;
});

const baseMapList = [
  //基础底图
  {
    name: '道路图',
    id: LAYERS_GROUP_ID.TD_BASE_MAP,
    tiles: TD_DL_URL,
    img: DaoLuTu,
  },
  {
    name: '影像图',
    id: LAYERS_GROUP_ID.TD_BASE_MAP,
    tiles: TD_IMG_URL,
    img: YingXiangTu,
  },
  {
    name: '标注图',
    id: LAYERS_GROUP_ID.TD_BASE_MAP,
    tiles: TD_CIA_URL,
    img: BiaoZhuTu,
  },
  {
    name: '户外图',
    id: LAYERS_GROUP_ID.TD_BASE_MAP,
    tiles: [],
    img: HuWaiTu,
  },
  {
    name: '深色图',
    id: LAYERS_GROUP_ID.TD_BASE_MAP,
    tiles: [],
    img: ShengSeTu,
  },
];

const changeBaseLayer = (val) => {
  //切换底图
  baseMapName.value = val.name;
  mapStore.SER_BASE_MAP_LAYER(val.name);

  let map = GET_MAPBOX_MAP();
  const layerId = val.id;
  let sources;

  if (map.getSource('mapbox-dem')) {
    map.setTerrain();
    map.removeSource('mapbox-dem');
  }

  // 移除原来图层
  REMOVE_MAPBOX_LAYER(layerId, map);

  if (val.name == '深色图') {
    MAP_STYLE_SWITCHER('mapbox/dark-v11', map);
  } else if (val.name == '户外图') {
    MAP_STYLE_SWITCHER('mapbox/outdoors-v12', map);
  } else {
    MAP_STYLE_RESET(map);
    sources = {
      TD_IMG: {
        id: layerId,
        type: 'raster',
        tiles: val.tiles,
        tileSize: 256,
      },
    };

    let layers = {
      id: layerId,
      type: 'raster',
      source: sources.TD_IMG,
    };

    setTimeout(() => {
      map.addLayerToGroup(layerId, layers);
    }, 100);

    if (mapStore.mode == '3d') {
      mapTo3d('2d');
    }
  }
};

const handleChangeShow = () => {
  emit('handleChangeIsBaseLayer');
};
</script>
<template>
  <div class="map-base-layer absolute z-20">
    <div class="line line-top-left"></div>
    <div class="line line-bottom-left"></div>
    <div class="line line-bottom-right"></div>
    <div class="bg"></div>
    <div class="content">
      <div class="top">
        <div class="bg-top"></div>
        <div class="text">基础底图</div>
        <div class="close-icon" @click="handleChangeShow()">
          <img :src="LayerClose" />
        </div>
      </div>
      <div class="box-content">
        <div class="layer-box p-7px">
          <div
            v-for="item in baseMapList"
            :key="item.name"
            class="layer-box-item"
            :class="baseMapName === item.name ? 'layer-box-item-active' : ''"
            @click="changeBaseLayer(item)"
          >
            <img :src="item.img" />
            <div>{{ item.name }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.map-base-layer {
  right: 85px;
  bottom: 30px;
  width: 260px;
  height: 250px;
  border-right: 2px solid rgb(38, 196, 251);
  border-left: 2px solid rgb(38, 196, 251);
  transition: 1s;
  .line {
    position: absolute;
    z-index: 2;
    width: 14px;
    height: 3px;
    background: rgba(38, 196, 251, 1);
  }
  .line-top-left {
    top: 0;
    left: 0;
  }
  .line-bottom-left {
    bottom: 0;
    left: 0;
  }
  .line-bottom-right {
    right: 0;
    bottom: 0;
  }
  .bg {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
    width: 100%;
    height: 100%;
    background: rgba(0, 19, 26, 0.7);
    opacity: 1;
    backdrop-filter: blur(10px);
  }
  .content {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
    width: 100%;
    height: 100%;
    color: rgba(255, 255, 255, 0.5);
    .top {
      width: 100%;
      height: 28px;
      padding-left: 13px;
      .text {
        position: absolute;
        z-index: 2;
        color: #fff;
        font-size: 14px;
        line-height: 28px;
      }

      .bg-top {
        position: absolute;
        top: 0;
        left: 0;
        z-index: 1;
        width: 100%;
        height: 28px;
        background: linear-gradient(90.44deg, rgba(38, 196, 251, 1) 0%, rgba(0, 13, 59, 0.19) 100%);
        opacity: 0.5;
      }
      .close-icon {
        position: absolute;
        top: -12px;
        right: -12px;
        z-index: 1;
        cursor: pointer;
      }
      img {
        display: block;
        width: 32px;
        height: 32px;
      }
    }
    .layer-box {
      display: flex;
      flex-wrap: wrap;

      .layer-box-item {
        margin: 7px 6px 0 6px;
        text-align: center;
        cursor: pointer;
        img {
          width: 61px;
          height: 61px;
          border: 1px solid rgba(38, 198, 251, 0);
        }
        div {
          width: 62px;
          overflow: hidden;
          font-size: 12px;
          line-height: 26px;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
      }
      .layer-box-item-active {
        img {
          border: 1px solid rgba(38, 196, 251, 1);
        }
        div {
          color: rgba(44, 199, 254, 1);
        }
      }
    }
  }
}
</style>
